import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { getResponse, createPagination } from 'utils/utils';
import { queryPurchaseAgent as _queryPurchaseAgent, savePurchaseAgent as _savePurchaseAgent, fetchUserList as _fetchUserList, deleteUser as _deleteUser, updateUser as _updateUser } from "../services/purchaseAgentService";
export default {
  namespace: 'purchaseAgent',
  state: {
    list: {},
    // 采购员信息列表
    pagination: {},
    // 采购员列表分页数据
    userList: [],
    // 用户列表
    userPagination: {} // 用户列表分页

  },
  effects: {
    // 获取采购员数据
    queryPurchaseAgent: /*#__PURE__*/_regeneratorRuntime.mark(function queryPurchaseAgent(_ref, _ref2) {
      var payload, put, call, res, list, pagination;
      return _regeneratorRuntime.wrap(function queryPurchaseAgent$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              _context.next = 4;
              return call(_queryPurchaseAgent, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);
              pagination = createPagination(list);

              if (!list) {
                _context.next = 11;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  list: list,
                  pagination: pagination
                }
              });

            case 10:
              return _context.abrupt("return", list);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, queryPurchaseAgent);
    }),
    // 新增或更新采购员
    savePurchaseAgent: /*#__PURE__*/_regeneratorRuntime.mark(function savePurchaseAgent(_ref3, _ref4) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function savePurchaseAgent$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_savePurchaseAgent, payload);

            case 4:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, savePurchaseAgent);
    }),
    fetchUserList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchUserList(_ref5, _ref6) {
      var payload, call, put, res, userPagination;
      return _regeneratorRuntime.wrap(function fetchUserList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context3.next = 4;
              return getResponse(call(_fetchUserList, payload));

            case 4:
              res = _context3.sent;
              userPagination = createPagination(res);

              if (!res) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  userList: res.content,
                  userPagination: userPagination
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchUserList);
    }),
    // 更新用户数据
    updateUser: /*#__PURE__*/_regeneratorRuntime.mark(function updateUser(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateUser$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_updateUser, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateUser);
    }),
    // 删除用户数据
    deleteUser: /*#__PURE__*/_regeneratorRuntime.mark(function deleteUser(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteUser$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_deleteUser, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteUser);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};